package com.diandiansong.app.utils;

import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;

import diandiansong.library.utils.CCallBack;
import diandiansong.library.utils.OnLoadingListener;

public class A extends Api {

    /**
     * /api/my/head
     */
    public static <T> void myHead(String url, OnLoadingListener oer, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("avatar", url);
        post("/api/my/head", params, oer, callBack);
    }

    /**
     * /**
     * GET /api/city
     * 城市地区
     */
    public static <T> void city(OnLoadingListener oer, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();

        get("/api/city", params, oer, callBack);
    }

    public static <T> void message(OnLoadingListener oer, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();

        get("/api/message", params, oer, callBack);
    }

    /**
     * POST /api/auth/register
     * 注册
     */
    public static <T> void register(String mobile,
                                    String verif_code,
                                    String password_one,
                                    String company,
                                    String realname,
                                    String province,
                                    String citys,
                                    String county,
                                    String address,
                                    String inviter_code,
                                    String license_img,
                                    String openid,
                                    OnLoadingListener oer, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", mobile);
        params.put("verif_code", verif_code);
        params.put("password_one", password_one);
        params.put("password_two", password_one);
        params.put("company", company);
        params.put("realname", realname);
        params.put("province", province);
        params.put("citys", citys);
        params.put("county", county);
        params.put("address", address);
        params.put("inviter_code", inviter_code);
        params.put("license_img", license_img);
        if (openid != null) {
            params.put("openid", openid);
        }

        post("/api/auth/register", params, oer, callBack);
    }

    /**
     * /api/auth/find_password
     * 忘记密码
     * 实现备注
     */
    public static <T> void authFind_password(String mobile,
                                             String verif_code,
                                             String password_one,
                                             OnLoadingListener oer, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", mobile);
        params.put("verif_code", verif_code);
        params.put("password_one", password_one);
        params.put("password_two", password_one);

        post("/api/auth/find_password", params, oer, callBack);
    }

    /**
     * POST /api/auth/find_password_sms
     * 忘记密码时发送手机验证码
     */
    public static <T> void authFind_password_sms(String mobile,
                                                 OnLoadingListener oer, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", mobile);

        post("/api/auth/find_password_sms", params, oer, callBack);
    }

    /**
     *
     */
    public static <T> void authSend_sms(String mobile,
                                        OnLoadingListener oer, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", mobile);

        post("/api/auth/send_sms", params, oer, callBack);
    }

    /**
     * POST /api/auth/login
     * 登录
     */
    public static <T> void login(String login_account, String password, OnLoadingListener oer,
                                 CCallBack<T> callBack) {

        try {
            // 从字符串中得到公钥
            PublicKey publicKey = RSAUtils.loadPublicKey(PUCLIC_KEY);
            // 从文件中得到公钥
            //InputStream inPublic = getResources().getAssets().open("rsa_public_key.pem");
            //PublicKey publicKey = RSAUtils.loadPublicKey(inPublic);
            // 加密
            byte[] encryptByte = RSAUtils.encryptData(password.getBytes(), publicKey);
            // 为了方便观察吧加密后的数据用base64加密转一下，要不然看起来是乱码,所以解密是也是要用Base64先转换
            password = Base64Utils.encode(encryptByte);

        } catch (Exception e) {
            e.printStackTrace();
        }


        Map<String, String> params = new HashMap<>();
        params.put("login_account", login_account);
        params.put("password", password);

        post("/api/auth/login", params, oer, callBack);
    }

    /**
     * POST /api/auth/openid
     * 微信登录openid判断
     */
    public static <T> void openid(String openid, OnLoadingListener oer,
                                  CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("openid", openid);

        post("/api/auth/openid", params, oer, callBack);
    }

    /**
     * GET /api/cart
     * 购物车首页
     */
    public static <T> void cart(String page, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("page", page);

        get("/api/cart", params, null, callBack);
    }

    /**
     * POST /api/cart/store
     * 加入购物车（保存）
     * product_id
     * (required)
     * 必填，商品ID
     * query	integer
     * goods_sum
     * (required)
     * 必填，购买的总数量，如果有多个规格，这里传的是多个规格相加的购买数量
     * query	integer
     * product_sku_id
     * 选填，规格的ID
     * query	integer
     */
    public static <T> void cartStore(String product_id, int goods_sum, String product_sku_id,
                                     CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("product_id", "" + product_id);
        params.put("goods_sum", "" + goods_sum);
        params.put("product_sku_id", "" + product_sku_id);

        post("/api/cart/store", params, null, callBack);
    }

    /**
     * /api/cart/del
     * 购物车删除
     */
    public static <T> void cartDel(String[] ids, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();

        StringBuilder id = new StringBuilder("[");

        for (int i = 0; i < ids.length; i++) {
            String s = ids[i];
            id.append("\"").append(s).append("\"");
            if (i != ids.length - 1) {
                id.append(",");
            }
        }
        id.append("]");
        params.put("id", id.toString());

        post("/api/cart/del", params, null, callBack);
    }


    /**
     * POST /api/cart/settle
     * 确认订单(包括立即购买)
     *
     * @param id             选填(购物车点击结算时必填)，json格式:[{'sku_sum':'8','id':'20731'},{'sku_sum':'8',
     *                       'id':'20732'}]，sku_sum是购物车页上的选择的数量，id是购物车列表规格所对应的cart_id
     *                       query	string
     * @param product_sku_id 选填(立即购买点击时必填)，规格的id，没有规格可以不需要传或者传0
     *                       query	string
     * @param goods_sum      选填(立即购买点击时必填)，购买商品的总数量
     *                       query	integer
     * @param product_id     选填(立即购买点击时必填)，商品id
     *                       query	integer
     * @param addr_id        选填，地址id，当选择其他地址时，需要传过来，要计算这个地址产生的运费
     *                       query	integer
     * @param callBack
     * @param <T>
     */
    public static <T> void cartSettle(
            String id,
            String product_sku_id,
            String goods_sum,
            String product_id,
            String addr_id,
            CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();

        if (id != null) {
            params.put("id", id);
        }
        if (product_sku_id != null) {

            params.put("product_sku_id", product_sku_id);
        }
        if (goods_sum != null) {

            params.put("goods_sum", goods_sum);
        }
        if (product_id != null) {

            params.put("product_id", product_id);
        }
        if (addr_id != null) {
            params.put("addr_id", addr_id);
        }

        post("/api/cart/settle", params, null, callBack);
    }

    public static <T> void cartSubmitorder(
            String addr_id,
            String orders,
            CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();

        params.put("addr_id", addr_id);
        params.put("orders", orders);

        post("/api/cart/submitorder", params, null, callBack);
    }

    public static <T> void payIndex(
            String order_id,
            String pay_method,
            CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();

        params.put("order_id", order_id);
        params.put("pay_method", pay_method);

        post("/api/pay/index", params, null, callBack);
    }

    /**
     * GET /api/commodity/info
     * 商品列表
     * cate_id
     * (required)
     * 必填，小类ID号
     * query	integer
     * page
     * 选填，分页的时候传输数组中是后一条数据的ID值，一页10条数据，默认为0
     * query
     */
    public static <T> void commodityInfo(String cate_id, String page, String brand_info, String breed_info, boolean best_selling, CCallBack<T>
            callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("cate_id", "" + cate_id);
        params.put("page", page);
        if (brand_info != null) {
            params.put("brand_info", brand_info);
        }
        if (breed_info != null) {
            params.put("breed_info", breed_info);
        }
        params.put("best_selling", best_selling ? "1" : "0");
        get("/api/commodity/info", params, null, callBack);
    }

    /**
     * GET /api/commodity/cate
     * 商品分类
     */
    public static <T> void commodityCate(CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();

        get("/api/commodity/cate", params, null, callBack);
    }

    /**
     * GET /api/commodity/view
     * 商品详情
     */
    public static <T> void commodityView(String id, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("id", id);

        get("/api/commodity/view", params, null, callBack);
    }

    public static <T> void commodityKill(CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        get("/api/commodity/kill", params, null, callBack);
    }

    /**
     * GET /api/index
     * 首页
     */
    public static <T> void index(CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();

        get("/api/index", params, null, callBack);
    }

    /**
     * GET /api/commodity/indextj
     * 首页三大类（新鲜果蔬、食品钜惠、鲜肉钜惠）
     * 实现备注
     */
    public static <T> void commodityIndextj(String types, String page, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("types", types);
        params.put("page", page);
        get("/api/commodity/indextj", params, null, callBack);
    }

    /**
     * GET /api/commodity/bestselling
     * 畅销榜
     * 实现备注
     */
    public static <T> void commodityBestselling(String page, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("page", page);
        get("/api/commodity/bestselling", params, null, callBack);
    }

    /**
     *
     */
    public static <T> void apiMyCommon_list(String page, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("page", page);
        get("/api/my/common_list", params, null, callBack);
    }

    /**
     * /api/news
     * 新闻列表
     */
    public static <T> void news(String page, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("page", page);

        get("/api/news", params, null, callBack);
    }

    public static <T> void myCollection(String page, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("page", page);

        get("/api/my/collection", params, null, callBack);
    }

    /**
     * GET /api/news/detail
     * 新闻详情
     */
    public static <T> void newsDetail(String id, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("id", id);

        get("/api/news/detail", params, null, callBack);
    }

    /**
     * GET /api/my
     * 首页
     */
    public static <T> void my(CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();

        get("/api/my", params, null, callBack);
    }

    /**
     *
     */
    public static <T> void commodityCollection(String id, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("id", id);

        post("/api/commodity/collection", params, null, callBack);
    }

    /**
     *
     */
    public static <T> void myCollection_del(String[] ids, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();

        StringBuilder id = new StringBuilder("[");

        for (int i = 0; i < ids.length; i++) {
            String s = ids[i];
            id.append("\"").append(s).append("\"");
            if (i != ids.length - 1) {
                id.append(",");
            }
        }
        id.append("]");
        params.put("id", id.toString());

        post("/api/my/collection_del", params, null, callBack);
    }

    /**
     * GET /api/address
     * 收货地址 - 列表
     */
    public static <T> void address(String page, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("page", page);

        get("/api/address", params, null, callBack);
    }

    /**
     * GET /api/address/insert
     * 收货地址 - 添加
     * names
     * (required)
     * 必填，姓名
     * query	string
     * mobile
     * (required)
     * 必填，手机号码
     * query	integer
     * province
     * (required)
     * 必填，省级ID
     * query	integer
     * citys
     * (required)
     * 必填，市级ID
     * query	integer
     * county
     * (required)
     * 必填，县区级ID
     * query	integer
     * addr
     * (required)
     * 必填，详情地址
     * query	string
     * is_default
     * 选填，是否默认 0=否 1=是
     * query	integer
     */
    public static <T> void addressInsert(String names,
                                         String mobile,
                                         String province,
                                         String citys,
                                         String county,
                                         String addr,
                                         boolean is_default,
                                         CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("names", names);
        params.put("mobile", mobile);
        params.put("province", province);
        params.put("citys", citys);
        params.put("county", county);
        params.put("addr", addr);
        params.put("is_default", is_default ? "1" : "0");

        post("/api/address/insert", params, null, callBack);
    }

    public static <T> void addressSave(String id,
                                       String names,
                                       String mobile,
                                       String province,
                                       String citys,
                                       String county,
                                       String addr,
                                       boolean is_default,
                                       CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("id", id);
        params.put("names", names);
        params.put("mobile", mobile);
        params.put("province", province);
        params.put("citys", citys);
        params.put("county", county);
        params.put("addr", addr);
        params.put("is_default", is_default ? "1" : "0");

        post("/api/address/save", params, null, callBack);
    }

    public static <T> void addressDel(String id,
                                      CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("id", id);

        post("/api/address/del", params, null, callBack);
    }

    /**
     *
     */
    public static <T> void orderIndex(String status, String page, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("page", page);
        params.put("status", status);

        get("/api/order/index", params, null, callBack);
    }

    /**
     *
     */
    public static <T> void orderShow(String order_id, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("order_id", order_id);

        get("/api/order/show", params, null, callBack);
    }

    /**
     *
     */
    public static <T> void orderCancel(String order_id, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("order_id", order_id);

        post("/api/order/cancel", params, null, callBack);
    }

    /**
     *
     */
    public static <T> void orderReceipt(String order_id, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("order_id", order_id);

        post("/api/order/receipt", params, null, callBack);
    }

    /**
     *
     */
    public static <T> void commoditySearch_record(CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();

        get("/api/commodity/search_record", params, null, callBack);
    }

    /**
     * POST /api/commodity/search_del
     */
    public static <T> void commoditySearch_del(CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();

        post("/api/commodity/search_del", params, null, callBack);
    }


    /**
     *
     */
    public static <T> void commoditySearch_info(String keyword, String page, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("keyword", keyword);
        params.put("page", page);

        get("/api/commodity/search_info", params, null, callBack);
    }

    /**
     * GET /api/commodity/brain_power
     */
    public static <T> void commodityBrain_power(String cate_id, CCallBack<T> callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("cate_id", cate_id);

        get("/api/commodity/brain_power", params, null, callBack);
    }
}